package xyz.chenyongpeng.orderconsumerdubbo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.chenyongpeng.entity.*;
import xyz.chenyongpeng.orderconsumerdubbo.vo.RelationSeriesVo;
import xyz.chenyongpeng.service.*;
import xyz.chenyongpeng.to.GoodsTo;
import xyz.chenyongpeng.to.MyCoupons;
import xyz.chenyongpeng.utils.HtmlUtil;
import xyz.chenyongpeng.utils.LowerPrice;

import java.util.*;

@Service
public class GoodService {

    @Autowired
    private IZsGoodsService zsGoodsService;

    @Autowired
    private IZsGTicketsPriceService zsGTicketsPriceService;

    @Autowired
    private IZsGoodsTypeService zsGoodsTypeService;

    @Autowired
    private IZsGTravelRouteService zsGTravelRouteService;

    @Autowired
    private IZsGTravelRouteGroupService zsGTravelRouteGroupService;

    @Autowired
    private IZsGTicketsService zsGTicketsService;

    @Autowired
    private IZsGAudioService zsGAudioService;

    @Autowired
    private IZsGComboAttrService zsGComboAttrService;

    @Autowired
    private IZsCouponService zsCouponService;

    @Autowired
    private IZsCouponIncludeGoodsService zsCouponIncludeGoodsService;

    public List<GoodsTo> getListByLimit(Map<String,Object> parms){
        return zsGoodsService.getListByLimit(parms);
    }

    public int getListCount(Map<String,Object> parms){
        return zsGoodsService.getListCount(parms);
    }

    public GoodsTo getOne(String goods_id) {
        Map<String, Object> parms = new HashMap<>();
        parms.put("goods_id", goods_id);
        return zsGoodsService.getOne(parms);
    }

    public ZsGoodsType getOneType(Integer gt_id) {
        return zsGoodsTypeService.getOne(new QueryWrapper<ZsGoodsType>().eq("gt_id", gt_id));
    }

    public ZsGTravelRoute getZsGTravelRoute(String goods_id) {
        return zsGTravelRouteService.getOne(new QueryWrapper<ZsGTravelRoute>().eq("goods_id", goods_id));
    }

    public ZsGTickets getZsGTickets(String goods_id) {
        return zsGTicketsService.getOne(new QueryWrapper<ZsGTickets>().eq("goods_id", goods_id));
    }

    public ZsGAudio getZsGAudio(String goods_id) {
        return zsGAudioService.getOne(new QueryWrapper<ZsGAudio>().eq("goods_id", goods_id));
    }

    public ZsGComboAttr getZsGComboAttr(String goods_id) {
        return zsGComboAttrService.getOne(new QueryWrapper<ZsGComboAttr>().eq("goods_id", goods_id));
    }

    public Map<String, Object> getGoodsAttr(String tabName, String goods_id) {
        Map<String, Object> parms = new HashMap<>();
        parms.put("tabName", tabName);
        parms.put("goods_id", goods_id);
        return zsGoodsService.getGoodsAttr(parms);
    }

    public Map<String, Object> buildLuxian(GoodsTo goodsTo, Map<String, Object> attr) {
        Map<String, Object> result = new HashMap<>();
        result.put("goods_id", goodsTo.getGoods_id());
        result.put("gt_id", goodsTo.getGt_id());
        result.put("goods_title", goodsTo.getGoods_title());
        result.put("goods_info", HtmlUtil.html2Text(goodsTo.getGoods_title()));
        result.put("goods_img", goodsTo.getGoods_img());
        result.put("goods_big_img", goodsTo.getGoods_big_img());
        result.put("price", goodsTo.getPrice());
        result.put("discount_amount", goodsTo.getDiscount_amount());
        result.put("ori_price", goodsTo.getOri_price());
        result.put("show_label", goodsTo.getShow_label());
        result.put("ticket_limit", goodsTo.getTicket_limit());
        result.put("assist_msg", goodsTo.getAssist_msg());
        result.put("recommend_reason", goodsTo.getRecommend_reason());
        result.put("ticket_category", goodsTo.getTicket_category());
        result.put("min", attr.get("min"));
        result.put("max", attr.get("min"));
        result.put("is_group", attr.get("is_group"));
        result.put("fee_purchase_note", attr.get("fee_purchase_note"));
        result.put("fee_include", attr.get("fee_include"));
        result.put("fee_exclude", attr.get("fee_exclude"));
        result.put("tip_refund_policy", attr.get("tip_refund_policy"));
        result.put("tip_import_note", attr.get("tip_import_note"));
        result.put("tip_service_msg", attr.get("tip_service_msg"));
        result.put("report_cut_time", attr.get("report_cut_time"));

        if (attr.get("is_group") == "1") {
            ZsGTravelRoute zsGTravelRoute = zsGTravelRouteService.getOne(new QueryWrapper<ZsGTravelRoute>().eq("goods_id", goodsTo.getGoods_id()));
            result.put("group", zsGTravelRoute);
        }

        xgGoods(attr, result);

        return result;
    }

    public Map<String, Object> buildTickets(GoodsTo goodsTo, Map<String, Object> attr) {
        Map<String, Object> result = new HashMap<>();
        result.put("goods_id", goodsTo.getGoods_id());
        result.put("gt_id", goodsTo.getGt_id());
        result.put("goods_title", goodsTo.getGoods_title());
        result.put("goods_info", goodsTo.getGoods_info());
        result.put("goods_img", goodsTo.getGoods_img());
        result.put("goods_big_img", goodsTo.getGoods_big_img());
        result.put("price", goodsTo.getPrice());
        result.put("ori_price", goodsTo.getOri_price());
        result.put("discount_amount", goodsTo.getDiscount_amount());
        result.put("show_label", goodsTo.getShow_label());
        result.put("ticket_limit", goodsTo.getTicket_limit());
        result.put("assist_msg", goodsTo.getAssist_msg());
        result.put("buy_cut_time", goodsTo.getBuy_cut_time());
        result.put("recommend_reason", goodsTo.getRecommend_reason());
        result.put("ticket_category", goodsTo.getTicket_category());
        result.put("fee_purchase_note", attr.get("fee_purchase_note"));
        result.put("fee_include", attr.get("fee_include"));
        result.put("fee_exclude", attr.get("fee_exclude"));
        result.put("tip_refund_policy", attr.get("tip_refund_policy"));
        result.put("tip_import_note", attr.get("tip_import_note"));
        result.put("tip_service_msg", attr.get("tip_service_msg"));
        result.put("is_appointment", goodsTo.getIs_appointment());

        xgGoods(attr, result);
        return result;
    }

    public Map<String, Object> buildCombo(GoodsTo goodsTo, Map<String, Object> attr) {
        Map<String, Object> result = new HashMap<>();
        result.put("goods_id", goodsTo.getGoods_id());
        result.put("gt_id", goodsTo.getGt_id());
        result.put("goods_title", goodsTo.getGoods_title());
        result.put("goods_info", goodsTo.getGoods_info());
        result.put("goods_img", goodsTo.getGoods_img());
        result.put("goods_big_img", goodsTo.getGoods_big_img());
        result.put("price", goodsTo.getPrice());
        result.put("ori_price", goodsTo.getOri_price());
        result.put("discount_amount", goodsTo.getDiscount_amount());
        result.put("show_label", goodsTo.getShow_label());
        result.put("ticket_limit", goodsTo.getTicket_limit());
        result.put("assist_msg", goodsTo.getAssist_msg());
        result.put("buy_cut_time", goodsTo.getBuy_cut_time());
        result.put("recommend_reason", goodsTo.getRecommend_reason());
        result.put("ticket_category", goodsTo.getTicket_category());
        result.put("fee_purchase_note", attr.get("fee_purchase_note"));
        result.put("fee_include", attr.get("fee_include"));
        result.put("fee_exclude", attr.get("fee_exclude"));
        result.put("tip_import_note", attr.get("tip_import_note"));
        result.put("tip_service_msg", attr.get("tip_service_msg"));
        result.put("tip_refund_policy", attr.get("tip_refund_policy"));

        xgGoods(attr, result);
        return result;
    }

    public Map<String, Object> buildOther(GoodsTo goodsTo, Map<String, Object> attr) {
        Map<String, Object> result = new HashMap<>();
        result.put("goods_id", goodsTo.getGoods_id());
        result.put("gt_id", goodsTo.getGt_id());
        result.put("goods_title", goodsTo.getGoods_title());
        result.put("goods_info", goodsTo.getGoods_info());
        result.put("goods_img", goodsTo.getGoods_img());
        result.put("goods_big_img", goodsTo.getGoods_big_img());
        result.put("price", goodsTo.getPrice());
        result.put("ori_price", goodsTo.getOri_price());
        result.put("discount_amount", goodsTo.getDiscount_amount());
        result.put("show_label", goodsTo.getShow_label());
        result.put("ticket_limit", goodsTo.getTicket_limit());
        result.put("assist_msg", goodsTo.getAssist_msg());
        result.put("buy_cut_time", goodsTo.getBuy_cut_time());
        result.put("recommend_reason", goodsTo.getRecommend_reason());
        result.put("ticket_category", goodsTo.getTicket_category());

        xgGoods(attr, result);
        return result;
    }

    public Map<String, Object> xgGoods(Map<String, Object> attr, Map<String, Object> goods) {
        Map<String, Object> relationMap = new HashMap<>();
        relationMap.put("limit", "0,15");
        relationMap.put("goods_id", Arrays.asList((attr.get("relation_series")).toString().split(",")));
        List<GoodsTo> goodsTos = zsGoodsService.getListByLimit(relationMap);
        List<RelationSeriesVo> relationSeriesVos = new ArrayList<>();
        for (GoodsTo g : goodsTos) {
            RelationSeriesVo vo = new RelationSeriesVo();
            vo.setGoods_id(g.getGoods_id().toString());
            vo.setGoods_title(g.getGoods_title());
            vo.setGoods_info(g.getGoods_info());
            vo.setGoods_img(g.getGoods_img());
            vo.setPrice(g.getPrice());
            vo.setDiscount_amount(g.getDiscount_amount());
            vo.setGt_id(g.getGt_id().toString());
            vo.setR_id(g.getR_id().toString());
            vo.setIs_appointment(g.getIs_appointment().toString());
            relationSeriesVos.add(vo);
        }

        Map<String, Object> recommondMap = new HashMap<>();
        recommondMap.put("limit", "0,15");
        recommondMap.put("goods_id", Arrays.asList((attr.get("recommend_series")).toString().split(",")));
        List<GoodsTo> recommonGoods = zsGoodsService.getListByLimit(recommondMap);
        List<RelationSeriesVo> recommondVos = new ArrayList<>();
        for (GoodsTo g : recommonGoods) {
            RelationSeriesVo vo = new RelationSeriesVo();
            vo.setGoods_id(g.getGoods_id().toString());
            vo.setGoods_title(g.getGoods_title());
            vo.setGoods_info(g.getGoods_info());
            vo.setGoods_img(g.getGoods_img());
            vo.setPrice(g.getPrice());
            vo.setDiscount_amount(g.getDiscount_amount());
            vo.setGt_id(g.getGt_id().toString());
            vo.setR_id(g.getR_id().toString());
            vo.setIs_appointment(g.getIs_appointment().toString());
            recommondVos.add(vo);
        }
        goods.put("relation_series", relationSeriesVos);
        goods.put("recommend_series", recommondVos);
        return goods;
    }

    /**
     * 轮播图
     * @param goods_id
     * @param data
     */
    public void getGoodsImg(String goods_id,Map<String,Object> data){
    }

    public List<ZsGTravelRouteGroup> getGroups(String goods_id){
        return zsGTravelRouteGroupService.list(new QueryWrapper<ZsGTravelRouteGroup>().eq("goods_id", goods_id));
    }

    /**
     * 获取一个门票的价格信息
     * @param goods_id
     * @return
     */
    public List<ZsGTicketsPrice> getAllTicketPrice(String goods_id){
        List<ZsGTicketsPrice> zsGTicketsPriceList = zsGTicketsPriceService.list(new QueryWrapper<ZsGTicketsPrice>().eq("goods_id", goods_id));
        for (ZsGTicketsPrice price:zsGTicketsPriceList) {
            price.setPrice(Integer.parseInt(LowerPrice.getLowerPrice(price.getPrice()+"")));
            price.setOriPrice(Integer.parseInt(LowerPrice.getLowerPrice(price.getOriPrice()+"")));
            price.setDiscountAmount(Integer.parseInt(LowerPrice.getLowerPrice(price.getDiscountAmount()+"")));
        }
        return zsGTicketsPriceList;
    }

    public List<MyCoupons> getCouponListByGoodsId(String goods_id,String gt_id,String u_id){
        Map<String,Object> parms = new HashMap<>();
        parms.put("goods_id", goods_id);
        parms.put("gt_id", gt_id);
        parms.put("u_id", u_id);
        List<ZsCouponIncludeGoods> zsCouponIncludeGoodsList = zsCouponIncludeGoodsService.list(new QueryWrapper<ZsCouponIncludeGoods>().eq("goods_id", goods_id));
        List<Integer> incloudeGoodIds = new ArrayList<>();
        for (ZsCouponIncludeGoods includeGoods:zsCouponIncludeGoodsList) {
            incloudeGoodIds.add(includeGoods.getCouponId());
        }
        parms.put("coupon_id", incloudeGoodIds);
        parms.put("nowTime", System.currentTimeMillis()/1000);
        return zsCouponService.getCouponListByGoodsId(parms);
    }
}
